package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.pojo.Result;
import com.itheima.pojo.StatusCode;
import com.itheima.service.CategoryMenuService;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.CategoryMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/categoryMenu")
public class CategoryMenuController {

    @Autowired
    private CategoryMenuService categoryMenuService;

    /**
     * 增加菜单
     * @param categoryMenuVO
     */
    @PostMapping("/add")
    public Result add(@RequestBody CategoryMenuVO categoryMenuVO) {

        try {
            categoryMenuService.add(categoryMenuVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"插入失败");
        }
        //封装返回对象
        return new Result(StatusCode.OK,"插入成功");
    }

    /**
     * 删除菜单
     * @param categoryMenuVO
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody CategoryMenuVO categoryMenuVO) {

        try {
            categoryMenuService.delete(categoryMenuVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"删除失败");
        }
        //封装返回对象
        return new Result(StatusCode.OK,"删除成功");
    }

    /**
     * 编辑菜单
     * @param categoryMenuVO
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CategoryMenuVO categoryMenuVO) {
        try {
            categoryMenuService.edit(categoryMenuVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"更新失败");
        }
        //封装返回对象
        return new Result(StatusCode.OK,"更新成功");
    }

    /**
     * 获取所有列表
     * @param keyword
     * @return
     */
    @GetMapping("/getAll")
    public Map<String,Object> getAll(@RequestParam(value = "keyword",required = false) String keyword) {

        List<Map<String,Object>> list;
        Map<String, Object> data;
        List<Map<String,Object>> list2=new ArrayList<>();

        if (keyword==null){
            list= categoryMenuService.getAll();
            //封装返回数据
            Map<String,Object> result=new HashMap<>();
            result.put("code","success");
            result.put("data",list);
            return result;
        }else {
            data= categoryMenuService.findAll(keyword);
            list2.add(data);
        }

        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data",list2);
        return result;
    }

    /**
     * 获取所有二级菜单-按钮列表
     * @param keyword
     * @return
     */
    @GetMapping("/getButtonAll")
    public Map<String,Object> getButtonAll(@RequestParam(value = "keyword",required = false) String keyword) {
        List<CategoryMenu> buttonAll = categoryMenuService.getButtonAll(keyword);
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data",buttonAll);
        return result;
    }

    /**
     * 获取菜单列表
     * @param categoryMenuVO
     * @return
     */
    @PostMapping("/getList")
    public Map<String,Object> getList(@RequestBody CategoryMenuVO categoryMenuVO) {
        PageInfo<CategoryMenu> pageInfo = categoryMenuService.getList(categoryMenuVO);
        //封装返回数据
        Map<String,Object> result=new HashMap<>();

        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",pageInfo.getList());
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",null);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);

        //将将data放入返回map
        result.put("code","success");
        result.put("data",data);

        //返回结果
        return result;
    }

    /**
     * 置顶菜单
     * @param categoryMenuVO
     */
    @PostMapping("/stick")
    public Result stick(@RequestBody CategoryMenuVO categoryMenuVO) {

        try {
            categoryMenuService.stick(categoryMenuVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"该菜单已经在顶上");
        }
        return new Result(StatusCode.OK,"置顶成功");
    }

}
